package cc.rengu.igas.channel.upgw.biz.service.impl;

import cc.rengu.igas.channel.upgw.common.enums.UpgwRespCodeEnum;
import cc.rengu.igas.channel.upgw.facade.UpgwPayService;
import cc.rengu.igas.channel.upgw.facade.request.*;
import cc.rengu.igas.channel.upgw.facade.response.*;
import cc.rengu.igas.channel.upgw.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

/**
 * @author wukefan
 * @version 1.0.0
 * @date 2020/9/27 21:57
 * 银联网关支付是实现类
 */
public class UpgwPayServiceImpl implements UpgwPayService {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<UpgwPersonalGatewayPaymentResponse> upgwPersonalGatewayPayment(UpgwPersonalGatewayPaymentRequest upgwPersonalGatewayPaymentRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<UpgwPersonalGatewayPaymentResponse> result = new Result<>();
        UpgwPersonalGatewayPaymentResponse upgwPersonalGatewayPaymentResponse = new UpgwPersonalGatewayPaymentResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(upgwPersonalGatewayPaymentRequest,
                    "UpgwPersonalGatewayPaymentService", UpgwPersonalGatewayPaymentResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((UpgwPersonalGatewayPaymentResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("银联网关支付-个人网关支付失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                upgwPersonalGatewayPaymentResponse.setRespCode(((BizException) e).getCode());
                upgwPersonalGatewayPaymentResponse.setRespDesc(e.getMessage());
            } else {
                upgwPersonalGatewayPaymentResponse.setRespCode(UpgwRespCodeEnum.SYSTEM_ERROR.getRespCode());
                upgwPersonalGatewayPaymentResponse.setRespDesc(UpgwRespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<UpgwEnterpriseGatewayPaymentResponse> upgwEnterpriseGatewayPayment(UpgwEnterpriseGatewayPaymentRequest upgwEnterpriseGatewayPaymentRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<UpgwEnterpriseGatewayPaymentResponse> result = new Result<>();
        UpgwEnterpriseGatewayPaymentResponse upgwEnterpriseGatewayPaymentResponse = new UpgwEnterpriseGatewayPaymentResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(upgwEnterpriseGatewayPaymentRequest,
                    "UpgwEnterpriseGatewayPaymentService", UpgwPersonalGatewayPaymentResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((UpgwEnterpriseGatewayPaymentResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("银联网关支付-企业网关支付失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                upgwEnterpriseGatewayPaymentResponse.setRespCode(((BizException) e).getCode());
                upgwEnterpriseGatewayPaymentResponse.setRespDesc(e.getMessage());
            } else {
                upgwEnterpriseGatewayPaymentResponse.setRespCode(UpgwRespCodeEnum.SYSTEM_ERROR.getRespCode());
                upgwEnterpriseGatewayPaymentResponse.setRespDesc(UpgwRespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<UpgwPersonalGatewayFullPaymentRefundResponse> upgwPersonalGatewayFullPaymentRefund(UpgwPersonalGatewayFullPaymentRefundRequest upgwPersonalGatewayFullPaymentRefundRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<UpgwPersonalGatewayFullPaymentRefundResponse> result = new Result<>();
        UpgwPersonalGatewayFullPaymentRefundResponse upgwPersonalGatewayFullPaymentRefundResponse = new UpgwPersonalGatewayFullPaymentRefundResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(upgwPersonalGatewayFullPaymentRefundRequest,
                    "UpgwPersonalGatewayFullPaymentRefundService", UpgwPersonalGatewayFullPaymentRefundResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((UpgwPersonalGatewayFullPaymentRefundResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("银联网关支付-个人支付退款失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                upgwPersonalGatewayFullPaymentRefundResponse.setRespCode(((BizException) e).getCode());
                upgwPersonalGatewayFullPaymentRefundResponse.setRespDesc(e.getMessage());
            } else {
                upgwPersonalGatewayFullPaymentRefundResponse.setRespCode(UpgwRespCodeEnum.SYSTEM_ERROR.getRespCode());
                upgwPersonalGatewayFullPaymentRefundResponse.setRespDesc(UpgwRespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<UpgwEnterpriseGatewayFullPaymentRefundResponse> upgwEnterpriseGatewayFullPaymentRefund(UpgwEnterpriseGatewayFullPaymentRefundRequest upgwEnterpriseGatewayFullPaymentRefundRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<UpgwEnterpriseGatewayFullPaymentRefundResponse> result = new Result<>();
        UpgwEnterpriseGatewayFullPaymentRefundResponse upgwEnterpriseGatewayFullPaymentRefundResponse = new UpgwEnterpriseGatewayFullPaymentRefundResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(upgwEnterpriseGatewayFullPaymentRefundRequest,
                    "UpgwEnterpriseGatewayFullPaymentRefundService", UpgwEnterpriseGatewayFullPaymentRefundResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((UpgwEnterpriseGatewayFullPaymentRefundResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("银联网关支付-个人支付退款失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                upgwEnterpriseGatewayFullPaymentRefundResponse.setRespCode(((BizException) e).getCode());
                upgwEnterpriseGatewayFullPaymentRefundResponse.setRespDesc(e.getMessage());
            } else {
                upgwEnterpriseGatewayFullPaymentRefundResponse.setRespCode(UpgwRespCodeEnum.SYSTEM_ERROR.getRespCode());
                upgwEnterpriseGatewayFullPaymentRefundResponse.setRespDesc(UpgwRespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<UpgwPersonalGatewayTransStateQueryResponse> upgwPersonalGatewayTransStateQuery(UpgwPersonalGatewayTransStateQueryRequest upgwPersonalGatewayTransStateQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<UpgwPersonalGatewayTransStateQueryResponse> result = new Result<>();
        UpgwPersonalGatewayTransStateQueryResponse upgwPersonalGatewayTransStateQueryResponse = new UpgwPersonalGatewayTransStateQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(upgwPersonalGatewayTransStateQueryRequest,
                    "UpgwPersonalGatewayTransStateQueryService", UpgwPersonalGatewayTransStateQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((UpgwPersonalGatewayTransStateQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("银联网关支付-个人支付退款失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                upgwPersonalGatewayTransStateQueryResponse.setRespCode(((BizException) e).getCode());
                upgwPersonalGatewayTransStateQueryResponse.setRespDesc(e.getMessage());
            } else {
                upgwPersonalGatewayTransStateQueryResponse.setRespCode(UpgwRespCodeEnum.SYSTEM_ERROR.getRespCode());
                upgwPersonalGatewayTransStateQueryResponse.setRespDesc(UpgwRespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<UpgwEnterpriseGatewayTransStateQueryResponse> upgwEnterpriseGatewayTransStateQuery(UpgwEnterpriseGatewayTransStateQueryRequest upgwEnterpriseGatewayTransStateQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<UpgwEnterpriseGatewayTransStateQueryResponse> result = new Result<>();
        UpgwEnterpriseGatewayTransStateQueryResponse upgwEnterpriseGatewayTransStateQueryResponse = new UpgwEnterpriseGatewayTransStateQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(upgwEnterpriseGatewayTransStateQueryRequest,
                    "UpgwPersonalGatewayTransStateQueryService", UpgwEnterpriseGatewayTransStateQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((UpgwEnterpriseGatewayTransStateQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("银联网关支付-个人支付退款失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                upgwEnterpriseGatewayTransStateQueryResponse.setRespCode(((BizException) e).getCode());
                upgwEnterpriseGatewayTransStateQueryResponse.setRespDesc(e.getMessage());
            } else {
                upgwEnterpriseGatewayTransStateQueryResponse.setRespCode(UpgwRespCodeEnum.SYSTEM_ERROR.getRespCode());
                upgwEnterpriseGatewayTransStateQueryResponse.setRespDesc(UpgwRespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }
}
